package com.yc.d0303;

import com.google.gson.Gson;
import com.yc.bean.Result;
import javafx.scene.chart.PieChart;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import javax.xml.crypto.Data;
import java.io.IOException;
import java.text.DateFormat;
import java.util.Date;

@WebServlet("/login.s")
public class LoginServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setCharacterEncoding("utf-8");
        resp.setContentType("application/json;charset=utf-8");

        Result result = new Result(0,"",null);
        //验证用户输入的验证码是否正确
        String ucode = req.getParameter("vcode");
        //获取对话中保存的验证码
        Object vcode = req.getSession().getAttribute("vcode");
        if (vcode.equals(ucode)==false){
            result.setMsg("验证码输入错误！");
        }else {
            String name = req.getParameter("name");
            String pwd = req.getParameter("pwd");
            if ("yc".equals(name) ||"zs".equals(name) ||"ls".equals(name) &&"123".equals(pwd)){
                result.setCode(1);
                result.setMsg("登录成功");
                //由于要显示登录事件，添加时间
                req.getSession().setAttribute("loginedTime",new Date().getTime());
                req.getSession().setAttribute("loginedUser",name);

                //获取登录前访问的页面
                Object requestURI = req.getSession().getAttribute("requestURI");
                result.setData(requestURI);
            }else {
                result.setMsg("用户名或密码错误！");
            }
        }
        String json = new Gson().toJson(result);
        resp.getWriter().append(json);

    }

}
